package com.cec.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cec.context.BaseContext;
import com.cec.dto.ActivityRegDTO;
import com.cec.dto.user.UserActivityRegDTO;
import com.cec.dto.user.VolunteerActivityRegDTO;
import com.cec.entity.ActivityReg;
import com.cec.entity.Volunteer;
import com.cec.exception.OperationDataException;
import com.cec.mapper.user.UserActivityRegMapper;
import com.cec.mapper.user.VolunteerActivityRegMapper;
import com.cec.result.Result;
import com.cec.result.ResultCode;
import com.cec.service.user.UserActivityRegService;
import com.cec.service.user.VolunteerActivityRegService;
import com.cec.vo.ActivityRegVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class VolunteerActivityRegServiceImpl extends ServiceImpl<VolunteerActivityRegMapper, Volunteer> implements VolunteerActivityRegService {


    @Override
    public Result saveVolunteerActivityReg(VolunteerActivityRegDTO volunteerActivityRegDTO) {
        Volunteer volunteer = new Volunteer();
        BeanUtils.copyProperties(volunteerActivityRegDTO,volunteer);
        volunteer.setUserId(BaseContext.getCurrentId());
        int i = baseMapper.insert(volunteer);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result queryActivityRegByVolunteerId() {
        QueryWrapper<Volunteer> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", BaseContext.getCurrentId());
        return Result.success(baseMapper.selectList(wrapper));
    }

    @Override
    public Result pageQueryVolunteerActivityReg(ActivityRegDTO activityRegDTO) {
        QueryWrapper<Volunteer> wrapper = new QueryWrapper<>();
        if (activityRegDTO.getActivityName() != null && !activityRegDTO.getActivityName().equals(""))
            wrapper.like("a.activity_name", activityRegDTO.getActivityName());
        if (activityRegDTO.getUserName() != null && !activityRegDTO.getUserName().equals(""))
            wrapper.like("u.user_name", activityRegDTO.getUserName());
        if (activityRegDTO.getStatus() != null)
            wrapper.eq("v.status", activityRegDTO.getStatus());
        wrapper.eq("v.deleted",0).orderByDesc("v.create_time")
                .eq("u.id", BaseContext.getCurrentId());
        IPage<ActivityRegVO> activityRegVOIPage = baseMapper.pageQueryVolunteerActivityReg(new Page<>(activityRegDTO.getPage(), activityRegDTO.getPageSize()), wrapper);
        log.info("activityVOIPage.getRecords()-->{}", activityRegVOIPage.getRecords());
        log.info("activityVOIPage.getTotal()-->{}", activityRegVOIPage.getTotal());
        return Result.success(activityRegVOIPage.getRecords(), activityRegVOIPage.getTotal());
    }
}
